package cn.kgc.vueebook2.controller;

import cn.kgc.vueebook2.pojo.Entry;
import cn.kgc.vueebook2.pojo.ResultEntity;
import cn.kgc.vueebook2.service.EntryService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-05-19 15:04
 */
@RestController
public class EntryController {

    @Resource
    EntryService entryService;

    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<Entry>> selectEntry(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pageSzie", defaultValue = "3", required = false) Integer pageSize,
            @RequestParam(value = "categoryId", defaultValue = "0", required = false) Integer categryId) {
        PageInfo<Entry> entryPageInfo = entryService.selectAllEntry(pageSize, pageNum, categryId);
        return ResultEntity.successWithoutData(entryPageInfo);
    }

    //前端post请求,参数是json必须添加@requestBody
    @RequestMapping("/saveEntry")//有id是修改 没id是添加
    public ResultEntity<Integer> saveEntry(@RequestBody Entry entry) {
        Integer integer = 0;
        if (entry.getId() == null) {//添加
            entryService.addEntry(entry);
        }else{//修改
            entryService.update(entry);
        }
        return ResultEntity.successWithoutData(integer);
    }

    //根据id查询图书
    @RequestMapping("/getEntryById")
    public ResultEntity<Entry> getEntryById(Integer id) {
        Entry entry = entryService.selectById(id);
        return ResultEntity.successWithoutData(entry);
    }

    //删除
    @RequestMapping("/delEntryById")
    public ResultEntity<Integer> delEntryById(Integer id){
        Integer integer = entryService.deleteEntry(id);
        return  ResultEntity.successWithoutData(integer);
    }

}
